package com.tianpengtech.common.ip;

import java.io.File;
import java.io.IOException;

import com.jfinal.kit.PathKit;

public class IpUtil {
	
	private static String file = PathKit.getWebRootPath()+File.separator+"WEB-INF"+File.separator+"ip2region.db";
	private static DbSearcher d = DbSearcherFactory.getDbSearcher(file);
	
	public static IpInfo getIpInfo(String ipAddress){
		IpInfo info = new IpInfo();
		try {
			DataBlock b= d.binarySearch(ipAddress);
			String result = b.getRegion();
			System.out.println(result);
			if(result!=null){
				result = result.replace("|",",");
				String ipArr[] = result.split(",");
				info.setCountry(ipArr[0]);
				info.setPart(ipArr[1]);
				info.setProvince(ipArr[2]);
				info.setCity(ipArr[3]);
				info.setProvider(ipArr[4]);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return info;
	}

	public static void main(String[] args) {
		IpInfo info = getIpInfo("123.56.186.144");
		System.out.println(info);
	}
}
